//package com.ruoyi.psi.base.product.service;
//
//
//import com.ruoyi.common.utils.StringUtils;
//import com.ruoyi.produce.material.mapper.ProduceMaterialMapper;
//import com.ruoyi.produce.process.receiving.mapper.ReceivingReportMapper;
//import com.ruoyi.psi.base.cost.domain.ProductionCostDto;
//import com.ruoyi.psi.base.cost.mapper.ProductionCostMapper;
//import com.ruoyi.psi.base.cost.service.IProductionCostService;
//import com.ruoyi.psi.base.product.domain.BaseProduce;
//import com.ruoyi.psi.base.product.domain.ProduceCheck;
//import com.ruoyi.psi.base.product.domain.ProductionProfit;
//import com.ruoyi.psi.base.product.mapper.BaseProduceMapper;
//import com.ruoyi.psi.base.producealarm.domain.ProduceAlarm;
//import com.ruoyi.psi.base.producealarm.mapper.ProduceAlarmMapper;
//import com.ruoyi.psi.base.warehouse.domain.BaseWarehouse;
//import com.ruoyi.psi.base.warehouse.mapper.BaseWarehouseMapper;
//import com.ruoyi.psi.inventory.check.mapper.InventoryCheckMapper;
//import com.ruoyi.psi.inventory.in.mapper.InventoryInMapper;
//import com.ruoyi.psi.inventory.out.mapper.InventoryOutMapper;
//import com.ruoyi.psi.inventory.transfer.mapper.InventoryTransferMapper;
//import com.ruoyi.psi.purchase.purchasereturn.mapper.PurchaseReturnMapper;
//import com.ruoyi.psi.purchase.storage.mapper.PurchaseStorageMapper;
//import com.ruoyi.psi.sale.delivery.mapper.SaleDeliveryMapper;
//import com.ruoyi.psi.sale.salereturn.mapper.SaleReturnMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.math.BigDecimal;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * 产品
// * 业务层处理
// * @author Drj 2021/6/28
// */
//@Service
//public class BaseProduceServiceImpl implements IBaseProduceService{
//
//    @Autowired
//    private BaseProduceMapper baseProduceMapper;
//    @Autowired
//    private BaseWarehouseMapper baseWarehouseMapper;
//    @Autowired
//    private ProduceAlarmMapper produceAlarmMapper;
//    @Autowired
//    private InventoryCheckMapper inventoryCheckMapper;
//    @Autowired
//    private InventoryInMapper inventoryInMapper;
//    @Autowired
//    private InventoryOutMapper inventoryOutMapper;
//    @Autowired
//    private InventoryTransferMapper inventoryTransferMapper;
//    @Autowired
//    private PurchaseStorageMapper purchaseStorageMapper;
//    @Autowired
//    private PurchaseReturnMapper purchaseReturnMapper;
//    @Autowired
//    private SaleReturnMapper saleReturnMapper;
//    @Autowired
//    private SaleDeliveryMapper saleDeliveryMapper;
//    @Autowired
//    private ProduceMaterialMapper produceMaterialMapper;
//    @Autowired
//    private ReceivingReportMapper receivingReportMapper;
//    @Autowired
//    private ProductionCostMapper productionCostMapper; //产品成本
//    @Autowired
//    private IProductionCostService productionCostService; //产品成本
//    /**
//     * 根据条件查询产品
//     *
//     * @param baseProduce 产品信息
//     * @return 产品集合
//     */
//    @Override
//    public List<BaseProduce> selectBaseProduceList(BaseProduce baseProduce) {
//        List<BaseProduce> produceList = baseProduceMapper.selectBaseProduceList(baseProduce);
//        List<BaseWarehouse> warehouseList = baseWarehouseMapper.selectBaseWarehouseList(new BaseWarehouse());
//        for (BaseProduce produce : produceList) {
//            for (BaseWarehouse baseWarehouse : warehouseList) {
//                //存入产品库存数量
//                produce.setProduceNumber(produce.getProduceNumber()+getProduceNumTotal(baseWarehouse.getWarehouseId(),produce.getProduceId()));
//            }
//        }
//        return produceList;
//    }
//
//    /**
//     * 统计产品库存数量
//     * @param warehouseId 仓库编号
//     * @param produceId 产品编号
//     * @return
//     */
//    public Double getProduceNumTotal(String warehouseId,String produceId){
//        BigDecimal produceNum = new BigDecimal("0");
//        ProduceCheck produceCheck = new ProduceCheck(produceId,warehouseId);
//        if (StringUtils.isNotNull(inventoryCheckMapper.getProduceNumberAfterCheck(warehouseId, produceId))){
//            //存在盘点后数量
//            //存入数量
//           produceCheck.setProduceCheckNum(inventoryCheckMapper.getProduceNumberAfterCheck(warehouseId, produceId).getProduceCheckNum());
//           //存入盘点截止时间
//           produceCheck.setInventoryDeadline(inventoryCheckMapper.getProduceNumberAfterCheck(warehouseId, produceId).getInventoryDeadline());
//        }
//        //最近一次库存盘点后产品数量
//        produceNum = produceNum.add(BigDecimal.valueOf(produceCheck.getProduceCheckNum()));
//        //存入其他入库数量
//        produceNum = produceNum.add(BigDecimal.valueOf(inventoryInMapper.getInventoryInProduceNum(produceCheck)));
//        //存入其他出库数量
//        produceNum = produceNum.subtract(BigDecimal.valueOf(inventoryOutMapper.getInventoryOutProduceNum(produceCheck)));
//        //存入库存调拨入库数量
//        produceNum = produceNum.add(BigDecimal.valueOf(inventoryTransferMapper.getTransferInProduceNum(produceCheck)));
//        //存入库存调拨出库数量
//        produceNum = produceNum.subtract(BigDecimal.valueOf(inventoryTransferMapper.getTransferOutProduceNum(produceCheck)));
//        //存入库存采购入库数量
//        produceNum = produceNum.add(BigDecimal.valueOf(purchaseStorageMapper.getPurchaseStorageProduceNum(produceCheck)));
//        //存入库存采购退货出库数量
//        produceNum = produceNum.subtract(BigDecimal.valueOf(purchaseReturnMapper.getPurchaseReturnProduceNum(produceCheck)));
//        //存入库存销售退回入库数量
//        produceNum = produceNum.add(BigDecimal.valueOf(saleReturnMapper.getSaleReturnProduceNum(produceCheck)));
//        //存入库存销售出库数量
//        produceNum = produceNum.subtract(BigDecimal.valueOf(saleDeliveryMapper.getSaleDeliveryProduceNum(produceCheck)));
//        //存入库存生产验收入库数量
//        produceNum = produceNum.add(BigDecimal.valueOf(receivingReportMapper.getReceivingReportProduceNum(produceCheck)));
//        //存入库存领/补/退料出库数量
//        produceNum = produceNum.subtract(BigDecimal.valueOf(produceMaterialMapper.getMaterialRequisitionProduceNum(produceCheck)));
//        return produceNum.doubleValue();
//    }
//
//
//    /**
//     * 根据id查询产品
//     *
//     * @param produceId 产品编号
//     * @return 产品
//     */
//    @Override
//    public BaseProduce selectBaseProduceById(String produceId) {
//        return baseProduceMapper.selectBaseProduceById(produceId);
//    }
//
//    /**
//     * 新增产品
//     *
//     * @param baseProduce 产品信息
//     * @return 结果
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public int insertBaseProduce(BaseProduce baseProduce) throws Exception{
//        if (StringUtils.isNotNull(baseProduceMapper.selectBaseProduceById(baseProduce.getProduceId()))){
//            //存在
//            return 0;
//        }
//        baseProduceMapper.insertBaseProduce(baseProduce);
//        List<BaseWarehouse> baseWarehouseList = baseWarehouseMapper.selectBaseWarehouseList(new BaseWarehouse());
//        for (int i = 0; i < baseWarehouseList.size(); i++) {
//            ProduceAlarm produceAlarm = new ProduceAlarm(baseProduce.getProduceId(), baseWarehouseList.get(i).getWarehouseId());
//            produceAlarmMapper.insertProduceAlarm(produceAlarm);
//        }
//        return 1;
//
//    }
//
//    /**
//     * 修改产品数据
//     *
//     * @param baseProduce 产品信息
//     * @return 结果
//     */
//    @Override
//    public int updateBaseProduce(BaseProduce baseProduce) {
//        if (!StringUtils.isNotNull(baseProduceMapper.selectBaseProduceById(baseProduce.getProduceId()))){
//            //不存在
//            return 0;
//        }
//        return baseProduceMapper.updateBaseProduce(baseProduce);
//    }
//
//    /**
//     * 删除功能
//     * 更新对应产品删除标志
//     *
//     * @param baseProduce 产品信息
//     * @return
//     */
//    @Override
//    public int updateBaseProduceDeleteFlag(BaseProduce baseProduce) {
//        return baseProduceMapper.updateBaseProduceDeleteFlag(baseProduce);
//    }
//
//    /**
//     * 查询所有产品的利润
//     * @param baseProduce
//     * @return
//     */
//    @Override
//    public List<ProductionProfit> selectProductionProfit(BaseProduce baseProduce) {
//        List<ProductionProfit> productionProfitList = new ArrayList<>();
//            // 查询所有产品信息
//            List<BaseProduce> baseProduces = baseProduceMapper.selectAllProduction(baseProduce);
////            System.out.println("产品信息" + baseProduces);
//            for (int i = 0; i < baseProduces.size(); i++) {
//                ProductionProfit productionProfit = new ProductionProfit(); //产品利润对象
//                String produceId = baseProduces.get(i).getProduceId();
//                productionProfit.setProduceId(produceId); //产品编号
//                productionProfit.setProduceName(baseProduces.get(i).getProduceName()); //产品名称
//                productionProfit.setBaseCategoryName(baseProduces.get(i).getBaseCategoryName()); //类别名称
//                productionProfit.setBaseUnit(baseProduces.get(i).getBaseUnit()); //计量单位名称
//                productionProfit.setProduceSpecs(baseProduces.get(i).getProduceSpecs()); //产品规格
//                productionProfit.setProduceBrand(baseProduces.get(i).getProduceBrand()); //产品品牌
//                //计算利润
//                ProductionCostDto productionCostDto = productionCostMapper.selectProductionCostById(produceId);
////                System.out.println("该产品的成本数据" + productionCostDto);
//                if (StringUtils.isNotNull(productionCostDto)) {
//                    //利润 = 销售单价 - 该产品成本
//                    Double profit = baseProduces.get(i).getProduceSalePrice() - productionCostDto.getTotalCost();
//                    productionProfit.setProfit(profit);
//                } else {
//                    // 利润 = 售价
//                    Double profit = baseProduces.get(i).getProduceSalePrice();
//                    productionProfit.setProfit(profit);
//                }
//                productionProfitList.add(productionProfit); //将当前产品利润对象添加到集合中
//            }
//        return productionProfitList;
//    }
//}
